//! By convention, main.zig is where your main function lives in the case that
//! you are building an executable. If you are making a library, the convention
//! is to delete this file and start with root.zig instead.
pub fn read_data_from_file(file_path: []const u8, allocator: std.mem.Allocator) ![]const u8 {
    const file = try std.fs.cwd().openFile(file_path, .{});
    defer file.close(); // 使用 defer 确保文件被关闭
    const file_stat = try file.stat();
    // std.debug.print("File size is: {} bytes\n", .{file_stat.size});

    // 3. 为 Reader 提供一个缓冲区
    var buffer: [81920]u8 = undefined; // 在栈上分配一个缓冲区

    // 4. 获取 Reader 接口实例，并传入缓冲区
    var file_reader = file.reader(&buffer); // 注意：这里需要传入缓冲区的引用
    const reader_interface = &file_reader.interface; // 获取实际的读取接口

    const content = try reader_interface.readAlloc(allocator, file_stat.size);
    return content;
    // defer allocator.free(content); // !!! 非常重要：必须释放 readAllAlloc 分配的内存 !!!
    // std.debug.print("Read all content: {s}\n", .{content});
}
fn test_rbtree() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    // var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = arena.allocator();
    // const DefaultRbtree = rbtree.DefaultRBTree;
    // const allocator = std.heap.c_allocator;
    const Tree = rbtree.DefaultRBTree(i32, f32);

    var tree = Tree.init(allocator, void{});
    defer tree.deinit();

    // insert some stuff into the tree
    var index: i32 = 0;
    while (index < 900000) : (index += 1) {
        const value = std.math.pow(
            f32,
            0.5,
            @floatFromInt(index),
        );
        // const value: f32 = @floatFromInt(index);
        try tree.put(index, value);
    }

    // std.debug.print("end test tree\n", .{});
}
// fn test_read_mesh() !void {
//     // const allocator = std.heap.c_allocator;
//     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
//     defer arena.deinit();
//     // var gpa = std.heap.GeneralPurposeAllocator(.{}){};
//     const allocator = arena.allocator();
//     const content = try read_data_from_file("data/bone.cell", allocator);
//     defer allocator.free(content);
//     // const allocator = std.testing.allocator;
//     var sm = try zlibcell.mesh_io.read_cell_str2smesh(
//         f32,
//         void,
//         void,
//         void,
//         content,
//         allocator,
//     );
//     defer sm.deinit(allocator);
//     std.debug.print("sm .cells len:{}\n", .{sm.data.simplex.cells.len});
//     var cm = try sm.transfer(
//         void,
//         void,
//         allocator,
//     );
//     defer cm.deinit();
//     std.debug.print("num c:{}\n", .{cm.num_cells()});
//     std.debug.print(" content size: {} \n", .{content.len});
// }

test "read mesh speed" {
    var timer = try std.time.Timer.start();

    //c++ bone.cell 5次 18秒，包括释放内存Mesh_free。
    // try test_rbtree();
    // try test_read_mesh();
    // const allocator = std.heap.c_allocator;
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    // var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = arena.allocator();
    const content = try read_data_from_file("data/bone.cell", allocator);
    defer allocator.free(content);
    // const allocator = std.testing.allocator;
    var sm = try zlibcell.mesh_io.read_cell_str2smesh(
        f32,
        void,
        void,
        void,
        content,
        allocator,
    );
    defer sm.deinit(allocator);
    std.debug.print("sm .cells len:{}\n", .{sm.data.simplex.cells.len});
    var cm = try sm.transfer(
        void,
        void,
        allocator,
    );
    defer cm.deinit();
    std.debug.print("num c:{}\n", .{cm.num_cells()});
    std.debug.print(" content size: {} \n", .{content.len});

    const elapsed = timer.read();
    std.debug.print(" 时间: {} ns\\n", .{elapsed});
}
pub fn main() !void {
    const a: usize = 3 / 2;
    const b: [3]u32 = .{ 1, 2, 3 };
    const c = &b;
    const c1 = c[0..0];
    std.debug.print("a:{} {}\n", .{ a, c1.len });

    // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
    // std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

    // stdout is for the actual output of your application, for example if you
    // are implementing gzip, then only the compressed bytes should be sent to
    // stdout, not any debugging messages.
    // const stdout_file = std.io.getStdOut().writer();
    // var bw = std.io.bufferedWriter(stdout_file);
    // const stdout = bw.writer();

    // try stdout.print("Run `zig build test` to run the tests.\n", .{});

    // try bw.flush(); // Don't forget to flush!
}

test "simple test" {
    var list = std.ArrayList(i32).empty;
    defer list.deinit(std.testing.allocator); // Try commenting this out and see if zig detects the memory leak!
    try list.append(std.testing.allocator, 42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

test "use other module" {
    try std.testing.expectEqual(@as(i32, 150), zlibcell.add(100, 50));
}

test "fuzz example" {
    const Context = struct {
        fn testOne(context: @This(), input: []const u8) anyerror!void {
            _ = context;
            // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
            try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
        }
    };
    try std.testing.fuzz(Context{}, Context.testOne, .{});
}

const std = @import("std");

/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
const zlibcell = @import("zlibcell");
// const rbtree = zlibcell.rbtree;
const rbtree = @import("rbtree");
